﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using _1._6._2015_Movie.Data;
using _1._6._2015_Movie.Models;
using _1._6._2015_Movie.Adapters;

namespace _1._6._2015_Movie.Controllers
{
    public class MovieController : Controller
    {
        private IMovieAdapter _adapter;

        public MovieController()
        {
            _adapter = MovieAdapterFactory.GetMovieAdapter();
        }

        public MovieController(IMovieAdapter adapter)
        {
            _adapter = adapter;
        }

        public ActionResult Create()
        {
            MovieViewModel model = new MovieViewModel();
            return View(model);
        }

        [HttpPost]
        public ActionResult Create(MovieViewModel model)
        {
            _adapter.CreateMovie(model);

            return RedirectToAction("Index");
        }

        public ActionResult Index()
        {
            MoviesViewModel model = new MoviesViewModel();
            model.Movies = _adapter.GetMovies();
            
            return View(model);
        }
        
        public ActionResult Detail(int  id)
        {
            MovieViewModel model = _adapter.GetMovie(id);
      
            return View(model);
        }

        public ActionResult Update(int id)
        {
            MovieViewModel model = _adapter.GetMovie(id);
            return View(model);
        }

        [HttpPost]
        public ActionResult Update(MovieViewModel model, int id)
        {
            model.MovieID = id;
            _adapter.UpdateMovie(model);
            return RedirectToAction("Index");
        }

        public ActionResult Delete(int id)
        {
            _adapter.DeleteMovie(id);

            return RedirectToAction("Index");
        }

        public ActionResult About()
        {
            ViewBag.Message = "Your application description page.";

            return View();
        }

        public ActionResult Contact()
        {
            ViewBag.Message = "Your contact page.";

            return View();
        }
    }
}